package com.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dao.cargo.*;
import com.domain.cargo.*;
import com.domain.vo.ExportProductResult;
import com.domain.vo.ExportResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ExportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class ExportServiceImpl implements ExportService {


    @Autowired
    private ContractDao contractDao;    //合同dao
    @Autowired
    private ContractProductDao contractProductDao;  //货物dao
    @Autowired
    private ExtCproductDao extCproductDao;  //附件dao


    @Autowired
    private ExportDao exportDao;    //报运单dao
    @Autowired
    private ExportProductDao exportProductDao;  //报运货物dao
    @Autowired
    private ExtEproductDao extEproductDao;  //报运附件dao

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    //通过合同生成,报运单 报运商品 报运附件,在报运单表中有合同ids的字段
    public void save(Export export) {
        //定义变量接收报运货物的总数和报运附件的总数
        Integer ExportProductNum = 0;
        Integer ExtEproductNum = 0;


        //设置报运单的id
        export.setId(UUID.randomUUID().toString());

        //通过ids获取所有合同对象的id
        String[] contractIds = export.getContractIds().split(",");
        //遍历所有的合同对象id,获取每个合同对象
        for (String contractId : contractIds) {
            Contract contract = contractDao.selectByPrimaryKey(contractId);
            //设置每个合同位 已报运 状态,更新
            contract.setState(2);
            contractDao.updateByPrimaryKeySelective(contract);

            //新建一个map,key为报运单id;value为报运货物id
            HashMap<String, String> map = new HashMap<>();

//--------------------------------------------------------------------------保存报运货物对象
            //每个合同都需要遍历货物和附件,通过合同id获取所有货物对象
            ContractProductExample contractProductExample = new ContractProductExample();
            contractProductExample.createCriteria().andContractIdEqualTo(contractId);
            List<ContractProduct> products = contractProductDao.selectByExample(contractProductExample);
            //遍历所有货物,添加到 报运货物
            for (ContractProduct product : products) {
                //new 报运货物 对象
                ExportProduct exportProduct = new ExportProduct();
                //copyProperties:类型和属性名一样,才复制值,此时id也一样,复制了
                BeanUtils.copyProperties(product, exportProduct);
                //将 报运货物id 覆盖, 报运货物的id在这里,因为报运单id是相同的
                exportProduct.setId(UUID.randomUUID().toString());
                //设置 报运货物 的报运单id
                exportProduct.setExportId(export.getId());
                //保存 报运货物
                exportProductDao.insertSelective(exportProduct);

                //保存货物id和报运货物id
                map.put(export.getId(), exportProduct.getId());

                //累加报运货物数量
                ExportProductNum += product.getCnumber();
            }
//--------------------------------------------------------------------------保存报运附件对象
            //通过合同id,获取所有的附加对象
            ExtCproductExample extCproductExample = new ExtCproductExample();
            extCproductExample.createCriteria().andContractIdEqualTo(contractId);
            List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
                BeanUtils.copyProperties(extCproduct, extEproduct);
                extEproduct.setId(UUID.randomUUID().toString());
                //设置报运附件的 报运单id
                extEproduct.setExportId(export.getId());

                //设置报运附件的 报运货物id,从哪里获取报运货物id, 从map中根据货物id取出,即是报运货物id
                String value = map.get(export.getId());
                extEproduct.setExportId(value);

                //保存 报运附件
                extEproductDao.insertSelective(extEproduct);

                //累加报运附件数量
                ExtEproductNum += extCproduct.getCnumber();
            }

        }

        //循环结束后, 要设置报运货物的总数 和 报运附件的总数, 所以定义变量进行,循环累加
        export.setProNum(ExportProductNum);
        export.setExtNum(ExtEproductNum);

        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);
        //因为前端,使用了name="exportProducts[${status.index}].cnumber"进行传参, 所以直接封装了exportProducts属性
        //直接获取 List<ExportProducts>
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts != null) {
            //遍历修改
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    //删除报运单, 就要删除所有的 报运货物和报运附件
    public void delete(String id) {
        //获取报运单
        Export export = exportDao.selectByPrimaryKey(id);
        //获取所有的报运货物
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductDao.selectByExample(exportProductExample);
        //遍历删除报运货物
        for (ExportProduct exportProduct : exportProducts) {
            exportProductDao.deleteByPrimaryKey(exportProduct.getId());
        }
        //获取所有报运附件
        ExtEproductExample extEproductExample = new ExtEproductExample();
        extEproductExample.createCriteria().andExportIdEqualTo(id);
        List<ExtEproduct> extEproducts = extEproductDao.selectByExample(extEproductExample);
        //遍历删除报运附件
        for (ExtEproduct extEproduct : extEproducts) {
            extEproductDao.deleteByPrimaryKey(extEproduct.getId());
        }
        //删除报运单
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
        //通过ExportResult获取报运单id
        String exportId = exportResult.getExportId();
        //通过exportId获取Export实体类
        Export export = exportDao.selectByPrimaryKey(exportId);
        //更新状态和备注
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        //更新数据库
        exportDao.updateByPrimaryKeySelective(export);

        //获取所有的货物
        Set<ExportProductResult> exportProductResults = exportResult.getProducts();
        //遍历,设置每个货物的 税
        for (ExportProductResult exportProductResult : exportProductResults) {
            ExportProduct exportProduct = new ExportProduct();
            //设置 税
            exportProduct.setTax(exportProductResult.getTax());
            //获取id,用来更新
            exportProduct.setId(exportProductResult.getExportProductId());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }
}
